﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using CallBox.Entity;
using CallBox.BL;

namespace CallBox.Services
{
    // NOTA: puede usar el comando "Rename" del menú "Refactorizar" para cambiar el nombre de clase "Services" en el código, en svc y en el archivo de configuración a la vez.
     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class Services : IServices
    {

         public clsMobile Mobile_Validate(String number, String password)
         {
            Boolean Ok = false;
            XmlDocument doc = new XmlDocument();
            String Numeros = "No found.";
            clsMobile objMObile = new clsMobile();
            if (!String.IsNullOrEmpty(number) && !String.IsNullOrEmpty(password))
            {
                 objMObile = clsMobileBL.Intancia.Validate(number, password);
                if (objMObile != null)
                {
                    Ok = true;
                    doc.LoadXml(SerializeToXml(objMObile, objMObile.GetType()));
                }
            }
            if (Ok == false)
            {
                doc.LoadXml(SerializeToXml(Numeros, Numeros.GetType()));
            }

            return objMObile;
        }
         
        public clsMobile Mobile_GetAuthCode(string number)
        {           
            clsMobile objMObile = new clsMobile();
            if (!String.IsNullOrEmpty(number))
            {
                objMObile = clsMobileBL.Intancia.GetAuthCode(number);                                
            }           

            return objMObile;
        }

        public string Mobile_ValidateFirstAccess(string number, string password)
        {
            String result = "";
            if (!String.IsNullOrEmpty(number) && !String.IsNullOrEmpty(password))
            {
                result = clsMobileBL.Intancia.ValidateFirstAccess(number, password);
            }

            return result;
        }

        public static string SerializeToXml(object Obj, System.Type ObjType)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            XmlSerializer ser = new XmlSerializer(ObjType);
            ser.Serialize(writer, Obj);
            return sb.ToString();
        }
    }
}
